﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SocialGame.Models;
using Ext.Net.MVC;
using Omu.ValueInjecter;
using Social.Game.DAL;
using SocialGame.Business;

namespace SocialGame.Areas.Games.Controllers
{
    public class FieldsController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public RestResult Create(FieldsModel field)
        {
            try
            {
                Field fieldDb = new Field();
                fieldDb.InjectFrom(field);
                fieldDb.FieldName = field.FieldName;
                DbUtil.Database.Insert(fieldDb);
                field.Id = fieldDb.ID;
                return new RestResult
                {
                    Success = true,
                    Message = "Tạo mới thành công",
                    Data = field
                };
            }
            catch (Exception e)
            {
                return new RestResult
                {
                    Success = false,
                    Message = e.Message
                };
            }
        }

        [AcceptVerbs(HttpVerbs.Get)]
        public RestResult Read()
        {
            try
            {
                var data = DbUtil.Database.Query<Field>("SELECT * FROM Fields");
                return new RestResult
                {
                    Success = true,
                    Data = from c in data.ToList()
                           select new FieldsModel()
                           {
                               Id = c.ID,
                               FieldName = c.FieldName
                           }
                };
            }
            catch (Exception e)
            {
                return new RestResult
                {
                    Success = false,
                    Message = e.Message
                };
            }
        }

        [AcceptVerbs(HttpVerbs.Put)]
        public RestResult Update(FieldsModel field)
        {
            try
            {
                Field fieldDb = new Field();
                fieldDb.InjectFrom(field);
                fieldDb.ID = field.Id.Value;
                DbUtil.Database.Update(fieldDb);
                return new RestResult
                {
                    Success = true,
                    Message = "Cập nhật thành công"
                };
            }
            catch (Exception e)
            {
                return new RestResult
                {
                    Success = false,
                    Message = e.Message
                };
            }
        }

        [AcceptVerbs(HttpVerbs.Delete)]
        public RestResult Destroy(int id)
        {
            try
            {
                DbUtil.Database.Delete("Fields", "ID", null, id);
                return new RestResult
                {
                    Success = true,
                    Message = "Xóa thành công!"
                };
            }
            catch (Exception e)
            {
                return new RestResult
                {
                    Success = false,
                    Message = e.Message
                };
            }
        }
    }

}
